package com.lucker.framework.filter;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Base64;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper{

	private final Logger LOGGER = LoggerFactory.getLogger(BodyReaderHttpServletRequestWrapper.class);
    private byte[] body;  
    private String encodeing = "UTF-8";
    StringBuffer stringBuffer = null;
    
    public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);  
        stringBuffer = new StringBuffer();
        BufferedReader bufferedReader = null;
        try {
        	bufferedReader = this.getRequest().getReader();
        	String val = null;
        	while((val = bufferedReader.readLine()) != null) {
        		stringBuffer.append(val);
        	}
        	body = stringBuffer.toString().getBytes();
        	//String result=new String(stringBuffer.toString().getBytes(),"utf-8");
        	
        	//System.out.println(body + "<----------" + stringBuffer.toString().getBytes() + "------------ > " + result);
        }catch(IOException e) {
        	LOGGER.error("RequestFilter.BodyReaderHttpServletRequestWrapper异常 : " , e );
        }finally {
			try {
				if(bufferedReader != null) {
					bufferedReader.close();
				}
			}catch(Exception e) {}
		}
    }  
  
    @Override  
    public BufferedReader getReader() throws IOException {  
        return new BufferedReader(new InputStreamReader(getInputStream()));  
    }  
  
    @Override  
    public ServletInputStream getInputStream() throws IOException {  
        final ByteArrayInputStream bais = new ByteArrayInputStream(body);  
        return new ServletInputStream() {  
  
            @Override  
            public int read() throws IOException {  
                return bais.read();  
            }  
        };  
    }  
    
    public String getBody() throws Exception {
    	
//    	String s = new String(stringBuffer.toString().getBytes(),"utf-8");
//    	
//    	String s2 = new String(stringBuffer.toString().getBytes("utf-8"));
//    	
//    	
//    	String s3 = new String(stringBuffer.toString().getBytes("utf-8"),"utf-8");
//    	
//    	String s4 = new String(body);
//    	
//    	System.out.println(">>>>>>>>>>>>>>>>>>>>>----------------------------------------------------"  + s);
//    	System.out.println(">>>>>>>>>>>>>>>>>>>>>----------------------------------------------------"  + s2);
//    	System.out.println(">>>>>>>>>>>>>>>>>>>>>----------------------------------------------------"  + s3);
//    	System.out.println(">>>>>>>>>>>>>>>>>>>>>----------------------------------------------------"  + new String(body));
    	return new String(body);
    }

}
